package com.dx.utils;

import com.dx.pojo.*;
import com.dx.pojo.armor.Armor;
import com.dx.pojo.armor.ArmorInfo;
import com.dx.pojo.legend.*;
import com.dx.pojo.weapon.Weapon;
import com.dx.pojo.weapon.WeaponInfo;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Component;


import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;



@Component
public class DestinyHtmlUtils {

    /**
     * 获取趋势武器
     * @return
     * @throws Exception
     */
    public  List<WeaponCard> getWeaponCard() throws Exception {
        List<WeaponCard> weaponCardList=new ArrayList<>();
        String url="https://destinytracker.com/destiny-2/db";
        Document document= Jsoup.parse(new URL(url),300000);
        Elements weaponCards = document.getElementsByClass("weapon-card");
        int index=0;
        for (Element weaponCard : weaponCards) {
            String linkId = weaponCard.getElementsByClass("weapon-card__link").attr("href");
            String weaponImg=weaponCard.getElementsByTag("img").attr("src");
            String weaponTitle = weaponCard.getElementsByClass("weapon-card__title").text();
            String weaponName=weaponCard.getElementsByClass("weapon-card__name").first().text();
            String weaponType=weaponCard.getElementsByClass("weapon-card__type").first().text();
            Elements weaponCardStat = weaponCard.getElementsByClass("weapon-card__stat");
            List statList=new ArrayList();
            for (Element stat : weaponCardStat) {
                String text = stat.text();
                String[] s = text.split(" ");
                statList.add(s[1]);
            }
            weaponCardList.add(new WeaponCard(linkId,index++,weaponTitle,weaponImg,weaponName,weaponType,statList));
        }
        return weaponCardList;

    }

    /**
     * 获取头部按钮
     * @return
     * @throws Exception
     */
    public List<String> getEntries() throws Exception {
        List<String> entriesList=new ArrayList<>();
        String url="https://destinytracker.com/destiny-2/db";
        Document document= Jsoup.parse(new URL(url),300000);
        Element entries = document.getElementsByClass("new-items__entries").first();
        Elements tagA = entries.getElementsByTag("a");
        for (Element element : tagA) {
            entriesList.add(element.text());
        }
        return entriesList;
    }

    /**
     * 获取武器列表
     * @param page 页码
     * @return
     * @throws Exception
     */
    public List<Weapon> getWeapon(String page) throws Exception {
        List<Weapon> weaponList=new ArrayList<>();
        String url="https://destinytracker.com/destiny-2/db/items/weapon?page="+page;
        Document document = Jsoup.parse(new URL(url), 30000);
        Element table = document.getElementsByClass("item-table").first();
        Elements item = table.getElementsByClass("item");
        int id=0;
        for (Element element : item) {
            String weaponId = element.getElementsByTag("a").attr("href");
            String imgUrl = element.getElementsByTag("img").attr("src");
            String weaponName = element.getElementsByClass("item__name").text();
            String tier = element.getElementsByClass("d2-tier-color").text();
            String type = element.getElementsByTag("span").get(1).text();
            String attribute = null;
            String attributeImg=null;
            if (element.getElementsByClass("item__subtypes").first().childrenSize()>2){
                attribute = element.getElementsByTag("span").get(2).text();
                attributeImg = element.getElementsByClass("item__subtypes").first()
                        .getElementsByTag("img").attr("src");

            }
            String stat = element.getElementsByClass("item__stat").text();
            String[] s = stat.split(" ");
            weaponList.add(new Weapon(id++,weaponId,imgUrl,weaponName,tier,type,attribute,attributeImg,s));
        }
        return weaponList;
    }

    /**
     * 获取武器详细信息
     * @return
     * @throws Exception
     */
    public WeaponInfo getWeaponInfo(String id) throws Exception{
        String url="https://destinytracker.com"+id;
        Document document = Jsoup.parse(new URL(url),30000);
        String weaponImg = document.getElementsByClass("item-header__hero").
                first().getElementsByTag("img").attr("src");
        String weaponIcon = document.getElementsByClass("item-header__icon").
                first().getElementsByTag("img").attr("src");
        String weaponName = document.getElementsByClass("item-header__details").
                first().getElementsByTag("h1").text();
        Element header__subtitle = document.getElementsByClass("item-header__subtitle").first();
        String level = header__subtitle.getElementsByTag("span").get(0).text();
        String type = header__subtitle.getElementsByTag("span").get(1).text();
        String damage_type=null;
        String damage_type_icon=null;
        if (header__subtitle.childrenSize()>2){
            damage_type=header__subtitle.getElementsByClass("damage-type").first().text();
            damage_type_icon=header__subtitle.getElementsByClass("damage-type").
                    first().getElementsByTag("img").attr("src");
        }
        String header__description = document.getElementsByClass("item-header__description").first().text();
        String header__lore=null;
        try{
            header__lore = document.getElementsByClass("item-header__lore").first().text();
        }catch (Exception e){

        }
        Element insights_card = document.getElementsByClass("insights card bordered header-bordered responsive").first();
        String matchName=null;
        try{
            matchName  = insights_card.getElementsByTag("h2").first().text();
        }catch (Exception e){
        }
        List<String[]> insights=new ArrayList<>();
        try {
            Elements insightsRow = document.getElementsByClass("insights__now-label");
            for (Element item : insightsRow) {
                String[] row = item.text().split(" ");
                insights.add(row);
            }

        }catch (Exception e){

        }
        Elements stats = document.getElementsByClass("stat");
        List<Stat> statList=new ArrayList<>();
        for (Element stat_item : stats) {
            String stat__name = stat_item.getElementsByClass("stat__name").first().text();
            String stat__value = stat_item.getElementsByClass("stat__value").first().text();
            String style=null;
            try {
                style=stat_item.getElementsByClass("progress progress--standard").
                        first().attr("style").
                        split(";")[0].split(":")[1];
                style=style.substring(0, style.length() - 1);
            }catch (Exception e){

            }
            statList.add(new Stat(stat__name,stat__value,style));
        }
        return new WeaponInfo(weaponImg,weaponIcon,weaponName,level,type,damage_type,damage_type_icon,
                header__description,header__lore,matchName,insights,statList);

    }

    /**
     * 获取传说
     * @return
     */
    public List<List<Legend>> getLegends(){
        String url="https://destinytracker.com/destiny-2/db/legends";
        List<List<Legend>> legendsCard=new ArrayList<>();

        try {
            Document document = Jsoup.parse(new URL(url), 30000);
            Elements items = document.getElementsByClass("legend card bordered header-bordered responsive");
            for (Element item : items) {
                Elements tagA = item.getElementsByTag("a");
                List<Legend> legendsItem=new ArrayList<>();
                for (Element a : tagA) {
                    String id = a.attr("href");
                    String legend_item__image = a.getElementsByTag("img").attr("src");
                    String legend_item__name = a.getElementsByClass("legend-item__name").first().text();
                    String legend_item__description=null;
                    if (a.childrenSize()>2){
                        legend_item__description = a.getElementsByClass("legend-item__description").first().text();
                    }
                    legendsItem.add(new Legend(id,legend_item__image,legend_item__name,legend_item__description));
                }
                legendsCard.add(legendsItem);
            }
            return legendsCard;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return legendsCard  ;
    }

    /**
     * 获取盔甲列表
     * @param page 页码
     * @return
     * @throws Exception
     */
    public List<Armor> getArmor(int page)throws Exception{
        List<Armor> armorList=new ArrayList<>();
        String url="https://destinytracker.com/destiny-2/db/items/armor?page="+page;
        Document document = Jsoup.parse(new URL(url), 30000);
        Element table = document.getElementsByClass("item-table").first();
        Elements items = table.getElementsByClass("item");
        for (Element item : items) {
            String id = item.getElementsByClass("item__icon").
                    first().getElementsByTag("a").attr("href");
            String linkId = item.getElementsByTag("a").attr("href");

            String item__icon = item.getElementsByClass("item__icon").
                    first().getElementsByTag("img").attr("src");
            String item__name = item.getElementsByClass("item__name").first().text();
            Elements item__subtypes = item.getElementsByClass("item__subtypes");
            String grade = null;
            String occupation=null;
            String position =null;
            for (Element item__subtype : item__subtypes) {
               grade = item__subtype.getElementsByTag("span").get(0).text();
               occupation = item__subtype.getElementsByTag("span").get(1).text();
               try{
                   position = item__subtype.getElementsByTag("span").get(2).text();

               }catch (Exception e){

               }
            }
            Elements item__stat = item.getElementsByClass("item__stat");
            List<String> stat=new ArrayList<>();
            for (Element element : item__stat) {
                stat.add(element.text());
            }
            armorList.add(new Armor(id,linkId,item__icon,item__name,grade,occupation,position,stat));
        }
        return armorList;

    }

    /**
     * 获取新异域武器
     * @return
     * @throws Exception
     */
    public List<Weapon> getNewExoticWeapons() throws Exception {
        List<Weapon> weaponList=new ArrayList<>();
        String url="https://destinytracker.com/destiny-2/db/items/weapon?t=6&s=14";
        Document document = Jsoup.parse(new URL(url), 30000);
        Element table = document.getElementsByClass("item-table").first();
        Elements item = table.getElementsByClass("item");
        int id=0;
        for (Element element : item) {
            String weaponId = element.getElementsByTag("a").attr("href");
            String imgUrl = element.getElementsByTag("img").attr("src");
            String weaponName = element.getElementsByClass("item__name").text();
            String tier = element.getElementsByClass("d2-tier-color").text();
            String type = element.getElementsByTag("span").get(1).text();
            String attribute = null;
            String attributeImg=null;
            if (element.getElementsByClass("item__subtypes").first().childrenSize()>2){
                attribute = element.getElementsByTag("span").get(2).text();
                attributeImg = element.getElementsByClass("item__subtypes").first()
                        .getElementsByTag("img").attr("src");

            }
            String stat = element.getElementsByClass("item__stat").text();
            String[] s = stat.split(" ");
            weaponList.add(new Weapon(id++,weaponId,imgUrl,weaponName,tier,type,attribute,attributeImg,s));
        }
        return weaponList;
    }

    /**
     * 获取新武器
     * @return
     * @throws Exception
     */
    public List<Weapon> getNewWeapons() throws Exception {
        List<Weapon> weaponList=new ArrayList<>();
        String url="https://destinytracker.com/destiny-2/db/items/weapon?s=14";
        Document document = Jsoup.parse(new URL(url), 30000);
        Element table = document.getElementsByClass("item-table").first();
        Elements item = table.getElementsByClass("item");
        int id=0;
        for (Element element : item) {
            String weaponId = element.getElementsByTag("a").attr("href");
            String imgUrl = element.getElementsByTag("img").attr("src");
            String weaponName = element.getElementsByClass("item__name").text();
            String tier = element.getElementsByClass("d2-tier-color").text();
            String type = element.getElementsByTag("span").get(1).text();
            String attribute = null;
            String attributeImg=null;
            if (element.getElementsByClass("item__subtypes").first().childrenSize()>2){
                attribute = element.getElementsByTag("span").get(2).text();
                attributeImg = element.getElementsByClass("item__subtypes").first()
                        .getElementsByTag("img").attr("src");

            }
            String stat = element.getElementsByClass("item__stat").text();
            String[] s = stat.split(" ");
            weaponList.add(new Weapon(id++,weaponId,imgUrl,weaponName,tier,type,attribute,attributeImg,s));
        }
        return weaponList;
    }

    /**
     * 获取新异域盔甲
     * @return
     * @throws Exception
     */
    public List<Armor> getNewExoticArmor()throws Exception{
        List<Armor> armorList=new ArrayList<>();
        String url="https://destinytracker.com/destiny-2/db/items/armor?t=6&s=14";
        Document document = Jsoup.parse(new URL(url), 30000);
        Element table = document.getElementsByClass("item-table").first();
        Elements items = table.getElementsByClass("item");
        for (Element item : items) {
            String id = item.getElementsByClass("item__icon").
                    first().getElementsByTag("a").attr("href");
            String linkId = item.getElementsByTag("a").attr("href");

            String item__icon = item.getElementsByClass("item__icon").
                    first().getElementsByTag("img").attr("src");
            String item__name = item.getElementsByClass("item__name").first().text();
            Elements item__subtypes = item.getElementsByClass("item__subtypes");
            String grade = null;
            String occupation=null;
            String position =null;
            for (Element item__subtype : item__subtypes) {
                grade = item__subtype.getElementsByTag("span").get(0).text();
                occupation = item__subtype.getElementsByTag("span").get(1).text();
                position = item__subtype.getElementsByTag("span").get(2).text();
            }
            Elements item__stat = item.getElementsByClass("item__stat");
            List<String> stat=new ArrayList<>();
            for (Element element : item__stat) {
                stat.add(element.text());
            }
            armorList.add(new Armor(id,linkId,item__icon,item__name,grade,occupation,position,stat));
        }
        return armorList;

    }

    /**
     * 获取新盔甲
     * @return
     * @throws Exception
     */
    public List<Armor> getNewArmor()throws Exception{
        List<Armor> armorList=new ArrayList<>();
        String url="https://destinytracker.com/destiny-2/db/items/armor?s=14";
        Document document = Jsoup.parse(new URL(url), 30000);
        Element table = document.getElementsByClass("item-table").first();
        Elements items = table.getElementsByClass("item");
        for (Element item : items) {
            String id = item.getElementsByClass("item__icon").
                    first().getElementsByTag("a").attr("href");
            String linkId = item.getElementsByTag("a").attr("href");
            String item__icon = item.getElementsByClass("item__icon").
                    first().getElementsByTag("img").attr("src");
            String item__name = item.getElementsByClass("item__name").first().text();
            Elements item__subtypes = item.getElementsByClass("item__subtypes");
            String grade = null;
            String occupation=null;
            String position =null;
            for (Element item__subtype : item__subtypes) {
                grade = item__subtype.getElementsByTag("span").get(0).text();
                occupation = item__subtype.getElementsByTag("span").get(1).text();
                position = item__subtype.getElementsByTag("span").get(2).text();
            }
            Elements item__stat = item.getElementsByClass("item__stat");
            List<String> stat=new ArrayList<>();
            for (Element element : item__stat) {
                stat.add(element.text());
            }
            armorList.add(new Armor(id,linkId,item__icon,item__name,grade,occupation,position,stat));
        }
        return armorList;

    }

    /**
     * 获取其他页
     * @param c
     * @param s
     * @return
     * @throws Exception
     */
    public List<Other> getNewItems(String c,String s)throws Exception{
        List<Other> list=new ArrayList<>();
        String url="https://destinytracker.com/destiny-2/db/items/inventory?c="+c+"&s="+s;
        Document document = Jsoup.parse(new URL(url), 30000);
        Elements list_item = document.getElementsByClass("list-item");
        for (Element item : list_item) {
            String id = item.getElementsByTag("a").attr("href");
            String item__icon = item.getElementsByClass("list-item__icon").attr("src");
            String item__name = item.getElementsByClass("list-item__name").text();
            String[] item__subtitle = item.getElementsByClass("list-item__subtitle").first().text().split(",");
            String item__description=null;
            try{
                item__description = item.getElementsByClass("list-item__description").text();
            }catch (Exception e){
                e.printStackTrace();
            }
            list.add(new Other(id,item__icon,item__name,item__subtitle,item__description));
        }
        return list;

    }

    /**
     * 获取新异域装备页
     * @return
     * @throws Exception
     */
    public List<Other> getNewExoticItems()throws Exception{
        List<Other> list=new ArrayList<>();
        String url="https://destinytracker.com/destiny-2/db/items/inventory?t=6&s=14";
        Document document = Jsoup.parse(new URL(url), 30000);
        Elements list_item = document.getElementsByClass("list-item");
        for (Element item : list_item) {
            String id = item.getElementsByTag("a").attr("href");
            String item__icon = item.getElementsByClass("list-item__icon").attr("src");
            String item__name = item.getElementsByClass("list-item__name").text();
            String[] item__subtitle = item.getElementsByClass("list-item__subtitle").first().text().split(",");
            String item__description=null;
            try{
                item__description = item.getElementsByClass("list-item__description").text();
            }catch (Exception e){
                e.printStackTrace();
            }
            list.add(new Other(id,item__icon,item__name,item__subtitle,item__description));
        }
        return list;

    }

    /**
     * 获取护甲详细信息
     * @return
     * @throws Exception
     */
    public ArmorInfo getArmorInfo(String id) throws Exception{
        String url="https://destinytracker.com"+id;
        Document document = Jsoup.parse(new URL(url),30000);
        String weaponImg = document.getElementsByClass("item-header__hero").
                first().getElementsByTag("img").attr("src");
        String weaponIcon = document.getElementsByClass("item-header__icon").
                first().getElementsByTag("img").attr("src");
        String weaponName = document.getElementsByClass("item-header__details").
                first().getElementsByTag("h1").text();
        Element header__subtitle = document.getElementsByClass("item-header__subtitle").first();
        String level = header__subtitle.getElementsByTag("span").get(0).text();
        String type = header__subtitle.getElementsByTag("span").get(1).text();
        String header__description = document.getElementsByClass("item-header__description").first().text();
        String header__lore=null;
        try{
            header__lore = document.getElementsByClass("item-header__lore").first().text();
        }catch (Exception e){

        }

        Elements stats = document.getElementsByClass("stat");
        List<Stat> statList=new ArrayList<>();
        for (Element stat_item : stats) {
            String stat__name = stat_item.getElementsByClass("stat__name").first().text();
            String stat__value = stat_item.getElementsByClass("stat__value").first().text();
            String style=null;
            try {
                style=stat_item.getElementsByClass("progress progress--standard").
                        first().attr("style").
                        split(";")[0].split(":")[1];
                style=style.substring(0, style.length() - 1);
            }catch (Exception e){

            }
            statList.add(new Stat(stat__name,stat__value,style));
        }

        return new ArmorInfo(weaponImg,weaponIcon,weaponName,level,type,header__description,header__lore,statList);
    }

    /**
     * 获取 Items Legend
     */
    public List<ItemsLegend> getItemsLegend(String id){
        String url="https://destinytracker.com"+id;
        List<ItemsLegend> itemsLegends=new ArrayList<>();
        try {
            Document document = Jsoup.parse(new URL(url), 30000);
            Elements node = document.getElementsByClass("legend-node");
            for (Element element : node) {
                String linkId = element.getElementsByClass("legend-node").first().attr("href");
                String nodeName=element.getElementsByClass("legend-node__name").first().text();;
                String nodeImg = "";
                String nodeDescription="";
                try{
                    nodeImg=element.getElementsByTag("img").first().attr("src");

                    nodeDescription = element.getElementsByClass("legend-node__description").first().text();
                }catch (Exception e){

                }
                itemsLegends.add(new ItemsLegend(linkId,nodeImg,nodeName,nodeDescription));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return itemsLegends;
    }

    /**
     * 获取SealsLegend详情
     * @param id 页码
     * @return
     */
    public List<SealsLegend> getSealsLegend(String id){
        String url="https://destinytracker.com"+id;
        List<SealsLegend> sealsLegend=new ArrayList<>();
        try {
            Document document = Jsoup.parse(new URL(url), 30000);
            Elements record = document.getElementsByClass("legend-record");
            for (Element item : record) {
                String recordImg = item.getElementsByClass("legend-record__image").first().attr("src");
                String recordName = item.getElementsByClass("legend-record__name").first().text();
                String recordDescription = item.getElementsByClass("legend-record__description").first().text();
                Element ul = item.getElementsByClass("legend-record__objectives").first();
                Elements li = ul.getElementsByTag("li");
                List<Objectives> objectivesList=new ArrayList<>();
                for (Element liItem : li) {
                    String objectives = liItem.getElementsByTag("span").get(0).text();
                    String requirement = liItem.getElementsByTag("span").get(1).text();
                    objectivesList.add(new Objectives(objectives,requirement));
                }
                sealsLegend.add(new SealsLegend(recordImg,recordName,recordDescription,objectivesList));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return sealsLegend;
    }

    /**
     * 获取itemLegend详情
     * @return
     */
    public List<ItemLegendDetail> getItemsLegendDetail(String id){
        String url="https://destinytracker.com"+id;
        List<ItemLegendDetail> detailList=new ArrayList<>();
        try {
            Document parse = Jsoup.parse(new URL(url), 30000);
            Elements legendItem = parse.getElementsByClass("list-item");
            for (Element element : legendItem) {
                String itemImgUrl = "";
                String itemName = "";
                String itemSubtitle ="";
                String itemDescription="";
                try{
                    itemImgUrl = element.getElementsByClass("list-item__icon").first().attr("src");
                    itemName = element.getElementsByClass("list-item__name").first().text();
                    itemSubtitle = element.getElementsByClass("list-item__subtitle").first().text();
                    itemDescription = element.getElementsByClass("list-item__description").first().text();
                }catch (Exception e){

                }
                detailList.add(new ItemLegendDetail(itemImgUrl,itemName,itemSubtitle,itemDescription));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return detailList;
    }

}
